package com.xjrsoft.module.taskSchedule.controller;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.common.helper.TsHelper;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.common.tsmodel.TsSchemeModel;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.taskSchedule.dto.GetTsPageListDto;
import com.xjrsoft.module.taskSchedule.dto.SavedSchemeInfoDto;
import com.xjrsoft.module.taskSchedule.entity.XjrTsProcess;
import com.xjrsoft.module.taskSchedule.entity.XjrTsScheme;
import com.xjrsoft.module.taskSchedule.entity.XjrTsSchemeInfo;
import com.xjrsoft.module.taskSchedule.service.IXjrTsProcessService;
import com.xjrsoft.module.taskSchedule.service.IXjrTsSchemeInfoService;
import com.xjrsoft.module.taskSchedule.service.IXjrTsSchemeService;
import com.xjrsoft.module.taskSchedule.vo.GetTsFormVo;
import com.xjrsoft.module.taskSchedule.vo.GetTsPageListVo;
import com.xjrsoft.module.taskSchedule.vo.SchemeInfoVo;
import com.xjrsoft.module.taskSchedule.vo.SchemeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 任务调度模板基础信息表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-11-10
 */
@RestController
@RequestMapping("/task-scheduling")
@AllArgsConstructor
@Api(value = "/task-scheduling",tags = "任务调度模块")
public class TaskSchedulingController {

    private IXjrTsProcessService processService;

    private IXjrTsSchemeInfoService schemeInfoService;

    private IXjrTsSchemeService schemeService;

    @GetMapping
    @ApiOperation(value = "获取任务信息列表 分页")
    public Response<PageOutput<GetTsPageListVo>> getPageList(GetTsPageListDto dto){
        return Response.ok(processService.getPageList(dto));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取任务信息详情")
    @ApiImplicitParam(name = "id", value = "id", dataType = "String")
    public Response<GetTsFormVo> getSchemeInfoById(@PathVariable String id) {

        XjrTsProcess process = processService.getById(id);
        GetTsFormVo vo = new GetTsFormVo();
        vo.setSchemeInfoVo(BeanUtil.copy(schemeInfoService.getById(process.getSchemeInfoId()), SchemeInfoVo.class));
        vo.setSchemeVo(BeanUtil.copy(schemeService.getById(process.getSchemeId()), SchemeVo.class));
        return Response.ok(vo);
    }

    @PostMapping
    @ApiOperation(value = "新增任务")
    @ApiImplicitParam(name = "dto", value = "dto", dataType = "SavedSchemeInfoDto")
    @SneakyThrows
    public Response<Boolean> addSchemeInfo(@RequestBody SavedSchemeInfoDto dto) {
        XjrTsSchemeInfo schemeInfo = BeanUtil.copy(dto.getSchemeInfoDto(), XjrTsSchemeInfo.class);
        XjrTsScheme scheme = BeanUtil.copy(dto.getSchemeDto(), XjrTsScheme.class);
        TsSchemeModel tsSchemeModel = JSONObject.parseObject(scheme.getScheme(), TsSchemeModel.class);
        String processId = IdWorker.get32UUID();
        if (processService.addTs(processId, schemeInfo, scheme)) {
            TsHelper.addJob(processId, tsSchemeModel);
            return Response.ok();
        }
        return Response.notOk();
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "修改任务")
    @SneakyThrows
    public Response updateSchemeInfo(@PathVariable String id, @RequestBody SavedSchemeInfoDto dto) {
        XjrTsProcess process = processService.getById(id);
        Integer state = process.getState();
        if (state == 3) {
            return Response.notOk("暂停中的任务不能修改！");
        }
        XjrTsSchemeInfo schemeInfo = BeanUtil.copy(dto.getSchemeInfoDto(), XjrTsSchemeInfo.class);
        XjrTsScheme scheme = BeanUtil.copy(dto.getSchemeDto(), XjrTsScheme.class);
        TsSchemeModel tsSchemeModel = JSONUtil.toBean(scheme.getScheme(), TsSchemeModel.class);
        if (processService.updateTs(process, schemeInfo, scheme)) {
            TsHelper.deleteJob(process.getId());
            TsHelper.addJob(process.getId(), tsSchemeModel);
            return Response.ok();
        }
        return Response.notOk();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除 根据schemeInfoid")
    public Response<Boolean> deleteSchemeInfo(@PathVariable String id) {
        XjrTsProcess process = processService.getById(id);
        Integer state = process.getState();
        if (state == 2) {
            return Response.notOk("运行中任务不能删除！");
        }
        return Response.ok(processService.deleteTs(id, process.getSchemeInfoId()));
    }

    @PutMapping("/{id}/start")
    @ApiOperation(value = "启用任务")
    public Response<Boolean> startTask(@PathVariable String id){
        XjrTsProcess process = processService.getById(id);
        Integer state = process.getState();
        if (state == 2) {
            return Response.notOk("运行中的任务不能启动！");
        } else if (state == 1) {
            return Response.notOk("该任务待执行中！");
        } else if (process.getEndTime() != null && process.getEndTime().isBefore(LocalDateTime.now())) {
            return Response.notOk("该任务已到达结束时间！");
        }
        return Response.ok(processService.startTs(id));
    }

    @PutMapping("/{id}/stop")
    @ApiOperation(value = "暂停任务")
    public Response<Boolean> stopTask(@PathVariable String id) {
        XjrTsProcess process = processService.getById(id);
        if (process.getState() != 2) {
            return Response.notOk("不在运行中的任务不能暂停！");
        }
        return Response.ok(processService.stopTask(id));
    }
}
